//
//  NZKTextView.m
//  LaSaCivilAffairsDepartment
//
//  Created by 聂志康 on 2019/7/30.
//  Copyright © 2019 wpkj. All rights reserved.
//

#import "NZKTextView.h"

@interface NZKTextView ()
/** 占位文字label */
@property (nonatomic, weak) UILabel *placeholderLabel;
@end

@implementation NZKTextView

- (UILabel *)placeholderLabel {
    if (!_placeholderLabel) {
        // 添加一个用来显示占位文字的label
        UILabel *placeholderLabel = [[UILabel alloc] init];
        placeholderLabel.numberOfLines = 0;
        CGRect placeholderLabelFrame = placeholderLabel.frame;
        placeholderLabelFrame.origin.x = 0;
        placeholderLabelFrame.origin.y = 10;
        placeholderLabel.frame = placeholderLabelFrame;
        [self addSubview:placeholderLabel];
        _placeholderLabel = placeholderLabel;
    }
    return _placeholderLabel;
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        // 垂直方向上永远有弹簧效果
        self.alwaysBounceVertical = YES;
        // 默认字体
        self.font = [UIFont systemFontOfSize:15];
        // 默认的占位文字颜色
        self.placeHolderColor = [UIColor grayColor];
        // 监听文字改变
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange) name:UITextViewTextDidChangeNotification object:nil];
    }
    return self;
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

/**
* 监听文字改变
*/
- (void)textDidChange {
    // 只要有文字, 就隐藏占位文字label
    self.placeholderLabel.hidden = self.hasText;
}

 /**
  * 更新占位文字的尺寸
  */
- (void)updatePlaceholderLabelSize {
    CGSize maxSize = CGSizeMake(ScreenWidth - 2 * CGRectGetMinX(self.placeholderLabel.frame), MAXFLOAT);
    CGRect placeholderLabelFrame = self.placeholderLabel.frame;
    self.placeholderLabel.frame = CGRectMake(placeholderLabelFrame.origin.x, placeholderLabelFrame.origin.y, [self.placeHolder boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : self.font} context:nil].size.width, [self.placeHolder boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : self.font} context:nil].size.height);
}

#pragma mark - 重写setter
- (void)setPlaceHolderColor:(UIColor *)placeHolderColor {
    _placeHolderColor = placeHolderColor;
    self.placeholderLabel.textColor = placeHolderColor;
}

- (void)setPlaceHolder:(NSString *)placeHolder {
    _placeHolder = [placeHolder copy];
    self.placeholderLabel.text = placeHolder;
    [self updatePlaceholderLabelSize];
}

- (void)setFont:(UIFont *)font {
    [super setFont:font];
    self.placeholderLabel.font = font;
    [self updatePlaceholderLabelSize];
}

- (void)setText:(NSString *)text {
    [super setText:text];
    [self textDidChange];
}

- (void)setAttributedText:(NSAttributedString *)attributedText {
    [super setAttributedText:attributedText];
    [self textDidChange];
}

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
// if (action == @selector(paste:))//禁止粘贴
// return NO;
// if (action == @selector(select:))// 禁止选择
// return NO;
// if (action == @selector(selectAll:))// 禁止全选
// return NO;
// return [super canPerformAction:action withSender:sender];
    UIMenuController *menuController = [UIMenuController sharedMenuController];
    if (menuController) {
        [UIMenuController sharedMenuController].menuVisible = NO;
    }
    return NO;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
